package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysAllowbid;
import com.ruoyi.system.domain.SysGoodparameter;
import com.ruoyi.system.domain.SysSupplier;
import com.ruoyi.system.mapper.SysAllowbidMapper;
import com.ruoyi.system.mapper.SysGoodparameterMapper;
import com.ruoyi.system.mapper.SysSupplierMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysBidinfoMapper;
import com.ruoyi.system.domain.SysBidinfo;
import com.ruoyi.system.service.ISysBidinfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 竞价信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-12-13
 */
@Service
public class SysBidinfoServiceImpl implements ISysBidinfoService 
{
    @Autowired
    private SysBidinfoMapper sysBidinfoMapper;
    @Autowired
    private SysAllowbidMapper sysAllowbidMapper;
    @Autowired
    private SysGoodparameterMapper sysGoodparameterMapper;
    @Autowired
    private SysSupplierMapper sysSupplierMapper;
    /**
     * 查询竞价信息
     * 
     * @param bidInfoId 竞价信息ID
     * @return 竞价信息
     */
    @Override
    public SysBidinfo selectSysBidinfoById(Long bidInfoId)
    {
        return sysBidinfoMapper.selectSysBidinfoById(bidInfoId);
    }

    /**
     * 查询竞价信息列表
     * 
     * @param sysBidinfo 竞价信息
     * @return 竞价信息
     */
    @Override
    public List<SysBidinfo> selectSysBidinfoList(SysBidinfo sysBidinfo)
    {
        return sysBidinfoMapper.selectSysBidinfoList(sysBidinfo);
    }

    /**
     * 新增竞价信息
     * 
     * @param sysBidinfo 竞价信息
     * @return 结果
     */
    @Override
    public int insertSysBidinfo(SysBidinfo sysBidinfo)
    {
        sysBidinfo.setCreateTime(DateUtils.getNowDate());
        int rowId = 0;

        SysGoodparameter sysGoodparameter = new SysGoodparameter();
        sysGoodparameter.setGoodsId(sysBidinfo.getGoodsId());
        sysGoodparameter.setAttribite2("1");
        List<SysGoodparameter> sysGoodparameterList = sysGoodparameterMapper.queryCurrentGoodParameter(sysGoodparameter);
        if(sysGoodparameterList.size()==1){
            sysBidinfo.setLowerestNumber(1);
        }
        if(sysGoodparameterList.size()>0){
            rowId = sysBidinfoMapper.insertSysBidinfo(sysBidinfo);
            SysAllowbid sysAllowbid = new SysAllowbid();
            for (SysGoodparameter sysGoodparameter1:sysGoodparameterList){

                sysAllowbid.setSupplierId(sysGoodparameter1.getSupplierId());
                sysAllowbid.setBidInfoId(sysBidinfo.getBidInfoId());
                sysAllowbidMapper.insertSysAllowbid(sysAllowbid);
            }
        }
        return rowId;
    }

    /**
     * 修改竞价信息
     * 
     * @param sysBidinfo 竞价信息
     * @return 结果
     */
    @Override
    public int updateSysBidinfo(SysBidinfo sysBidinfo)
    {
        return sysBidinfoMapper.updateSysBidinfo(sysBidinfo);
    }

    /**
     * 删除竞价信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysBidinfoByIds(String ids)
    {
        return sysBidinfoMapper.deleteSysBidinfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除竞价信息信息
     * 
     * @param bidInfoId 竞价信息ID
     * @return 结果
     */
    @Override
    public int deleteSysBidinfoById(Long bidInfoId)
    {
        return sysBidinfoMapper.deleteSysBidinfoById(bidInfoId);
    }
    /**
     * 根据采购计划商品ID查询竞价信息表实体
     *
     * @param purchaseItemId1
     * @return
     */
    @Override
    public SysBidinfo findPurchaseitemId(Long purchaseItemId1) {
        return sysBidinfoMapper.findPurchaseitemId(purchaseItemId1);
    }

    /**
     * 开启竞价保存方法
     * @param sysBidinfo
     * @param goodsId
     * @return
     */
    @Override
    public SysBidinfo save(SysBidinfo sysBidinfo, Long goodsId) {
        SysGoodparameter sysGoodparameter = new SysGoodparameter();
        sysGoodparameter.setGoodsId(sysBidinfo.getGoodsId());
        sysGoodparameter.setAttribite2("1");

        List<SysGoodparameter> sysGoodparameterList = sysGoodparameterMapper.queryCurrentGoodParameter(sysGoodparameter);

        if(sysGoodparameterList.size()>0){
            sysBidinfo = sysBidinfoMapper.insertBindInfo(sysBidinfo);
            SysAllowbid allowBid = new SysAllowbid();
            String moble="";
            for (SysGoodparameter goodParameter : sysGoodparameterList) {
                allowBid.setSupplierId(goodParameter.getSupplierId());
                allowBid.setBidInfoId(sysBidinfo.getBidInfoId());

                sysAllowbidMapper.insertSysAllowbid(allowBid);
                SysSupplier supplier = sysSupplierMapper.selectSysSupplierById(goodParameter.getSupplierId());
                if(supplier!=null){
                    moble+=supplier.getMoble()+",";
                }
            }
            moble = moble.substring(0, moble.length()-1);
            sysBidinfo.setAttribute3(moble);
        }else{
            sysBidinfo.setAttribute3("0");
        }

        return sysBidinfo;
    }
}
